<?php
/**
 * This file is part of won-uploader.
 *
 * Author: Daniel K.
 *
 * won-uploader is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * won-uploader is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with won-uploader. If not, see <http://www.gnu.org/licenses/>.
 */

 define("won-uploader","true");
 session_start();
 
 // Include Paths
 require_once dirname(__FILE__)."/lib/Paths.lib.php";
 
 // Create new Paths object
 $_paths = new Paths();
 
 // Include required files
 require_once $_paths->getConfigFile();
 require_once $_paths->getIncludePath("Security");
 require_once $_paths->getIncludePath("Exceptions");
 require_once $_paths->getIncludePath("NanoQuery");
 require_once $_paths->getIncludePath("Document");
 require_once $_paths->getIncludePath("TemplateLoader");
 require_once $_paths->getIncludePath("LanguagePool");

 // Database connection
 $_nano = new NanoQuery(DB_USER, DB_PASS, DB_NAME, DB_HOST);
 $_nano->useUTF8();
 
 // Check language first
 if( isset($_GET['l']) && !empty($_GET['l']) )
 {
 	// Security
 	$lang = str_replace("..", "", $_GET['l']);
 	
 	if( !file_exists($_paths->getLanguageFile($lang)) )
 		$_SESSION['language'] = 'en';
 	else
 		$_SESSION['language'] = $lang;
 	
 }
 elseif( !isset($_SESSION['language']) || empty($_SESSION['language']) )
 	$_SESSION['language'] = 'en';
 
 $_language = new LanguagePool($_SESSION['language']);
 
 // Check for logout
 if( isset($_GET['logout']) )
 {
 	$lang = $_SESSION['language'];	// Save language
 	
 	Security::logout();
 	
 	header("Location: ".$_paths->getHTTPURL()."?l=$lang");
 	die();
 }
 
 
 try
 {
 	$_language->initialize();
 	Security::loadSession();
 }
 catch( Exception $ex )
 {
 	if( $ex->getCode() == Exceptions::USER_NOT_LOGGED_IN )
 	{
 		require_once $_paths->getLoginPath();
 		die();
 	}
 	elseif( $ex->getCode() == Exceptions::LANGUAGE_FILE_NOT_FOUND )
 	{
 		die("<h3>Language file for <u>".$_language->getLanguage()."</u> not found.</h3>");
 	}
 	
 	die($ex->getMessage());
 }
 
 // Application document
 $_document = new Document();
 
 // Load content file
 require_once "testfile.php";
 
 $loader = new TemplateLoader();
 
 try
 {
 	$loader->loadTemplate("main");
 }
 catch( Exception $ex )
 {
 	if( $ex->getCode() == Exceptions::TEMPLATE_404 )
 	{
 		die("The template could not be found");
 	}
 	else
 	{
 		die($ex->getMessage());
 	}
 }
?>